package tdd.application;

import tdd.domain.Employee;
import tdd.domain.EmployeeRepository;
import tdd.infrastructure.NotifyService;

import java.time.LocalDate;
import java.util.List;

public class BirthdayService {
    private EmployeeRepository employeeRepository;

    private NotifyService notifyService;

    public BirthdayService(EmployeeRepository employeeRepository, NotifyService notifyService) {
        this.employeeRepository = employeeRepository;
        this.notifyService = notifyService;
    }

    public void sendGreeting(LocalDate birthday) {
        // 加载所有员工
        List<Employee> employees = employeeRepository.findAllEmployees();

        //生日筛选并发送生日祝贺
        employees.stream()
                .filter(e -> isSameMonthDay(e.getBirthdayWithDefaultFormatter(), birthday))
                .forEach(e -> notifyService.invoke(e.getEmail(), e.getFirstName()));
    }

    private boolean isSameMonthDay(LocalDate date, LocalDate compareDate){
        return date.getMonth().equals(compareDate.getMonth())
                && date.getDayOfMonth() == compareDate.getDayOfMonth();
    }

}
